package com.haiqiu.tools.excel.style;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFShape;

import java.util.List;
import java.util.Map;

/**
 * @author HaiQiu
 * @since 2022/10/27
 */
public class StyleUtils {

    /**
     * 设置批注
     *
     * @param wb    wb
     * @param sheet sheet
     * @param row   row
     * @param cell  cell
     * @param desc  批注说明
     * @return 批注
     */
    public static Comment getComment(SXSSFWorkbook wb, SXSSFSheet sheet, SXSSFRow row, SXSSFCell cell, String desc) {
        //设置批注
        Drawing<XSSFShape> drawing = sheet.createDrawingPatriarch();
        // 当评论框可见时，让它显示在 1x3 的空间中
        CreationHelper factory = wb.getCreationHelper();
        ClientAnchor anchor = factory.createClientAnchor();
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex() + 1);
        anchor.setRow1(row.getRowNum());
        anchor.setRow2(row.getRowNum() + 3);
        // 创建评论并设置文本+作者
        Comment comment = drawing.createCellComment(anchor);
        RichTextString str = factory.createRichTextString(desc);
        comment.setString(str);
        comment.setAuthor("HaiQiu");
        return comment;
    }

    /**
     * 表头样式
     *
     * @param wb wb
     * @return 表头样式
     */
    public static CellStyle getTitleStyle(SXSSFWorkbook wb) {
        CellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);//垂直对齐方式
        style.setVerticalAlignment(VerticalAlignment.CENTER);//水平居中
        //设置背景颜色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = wb.createFont();
        font.setBold(true);
        //设置字体颜色
        font.setColor(IndexedColors.BLACK1.getIndex());
        style.setFont(font);
        style.setWrapText(true);// 开启换行
        //边框
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        return style;
    }

    /**
     * 设置合并标题样式
     *
     * @param wb wb
     * @return 设置合并标题样式
     */
    public static CellStyle getHeaderStyle(SXSSFWorkbook wb) {
        //设置标题样式
        CellStyle style = wb.createCellStyle();
        //设置背景颜色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置自动换行
        Font font = wb.createFont();
        font.setFontHeightInPoints((short) 15);//设置字体大小
        //设置字体颜色
        font.setColor(IndexedColors.BLACK1.getIndex());
        font.setBold(true);
        style.setFont(font);
        //对齐方式
        //设置水平对齐方式
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置垂直对齐方式
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //边框
        style.setBorderBottom(BorderStyle.THIN); //下边框
        style.setBorderLeft(BorderStyle.THIN);//左边框
        style.setBorderTop(BorderStyle.THIN);//上边框
        style.setBorderRight(BorderStyle.THIN);//右边框
        return style;
    }

    /**
     * 设置合并列的样式和字体颜色
     *
     * @param sheetName 名称
     * @param maps      数据
     * @param workbook  工作薄
     * @param sheet     sheet
     * @param rowHeader 行
     */
    public static Map<String, Object> getHeaderToMap(List<Map<String, Object>> maps, String sheetName, SXSSFWorkbook workbook, SXSSFSheet sheet, SXSSFRow rowHeader) {
        rowHeader.setHeight((short) (12 * 80));
        SXSSFCell cellHeader = rowHeader.createCell(0);
        // 设置标题样式
        CellStyle headerStyle = StyleUtils.getHeaderStyle(workbook);
        cellHeader.setCellStyle(headerStyle);
        cellHeader.setCellValue(sheetName);
        //合并单元格
        Map<String, Object> map = maps.get(0);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, map.size() - 1));
        //设置批注
        Comment comment = StyleUtils.getComment(workbook, sheet, rowHeader, cellHeader, sheetName);
        // 将注释分配给单元格
        cellHeader.setCellComment(comment);
        return map;
    }
}
